/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.gp.ui.image_view;

import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.zip.ZipException;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileFilter;

import org.gp.demo.file.pdf.ImagePdf;
import org.gp.demo.file.zip.ZipFileOpter;

/**
 * 
 * @author Administrator
 */
public class ImageViewUI extends javax.swing.JFrame {
	private static final int DEFAULT_WIDTH = 800;
	private static final int DEFAULT_HEIGHT = 600;
	private JTextArea fileText;

	/**
	 * Creates new form ImageViewUI
	 */
	public ImageViewUI() {
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {
		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		fileText = new JTextArea();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		fileText.setText("");

		JMenuBar menuBar = new JMenuBar();
		JMenu menu = new JMenu("File");

		JMenuItem selectItem = new JMenuItem("选择文件");
		menu.add(selectItem);

		JMenuItem openItem = new JMenuItem("打开文件");
		menu.add(openItem);
		// 注册监听
		selectItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				// 文件选择框
				JFileChooser chooser = new JFileChooser();
				chooser.setMultiSelectionEnabled(true);
				// 添加文件过滤
				chooser.setFileFilter(new FileFilter() {
					// 只能选择文件夹或者zip文件
					public boolean accept(File f) {
						return f.isDirectory() || f.getName().endsWith(".zip");
					}

					@Override
					public String getDescription() {
						// TODO Auto-generated method stub
						return "Zip";
					}
				});
				chooser.setCurrentDirectory(new File("E:/TDDOWNLOAD/视频/动漫/烙印战士"));
				int r = chooser.showOpenDialog(ImageViewUI.this);
				if (r == JFileChooser.APPROVE_OPTION) {
					try {
						ZipFileOpter.getZipFileData(chooser.getSelectedFile()
								.getAbsolutePath());
						ImagePdf.createPDF();
						// fileText.append(ZipFileOpter.getZipFileView(chooser
						// .getSelectedFile().getAbsolutePath()));
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}

		});

		// 注册监听
		openItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				// 文件选择框
				JFileChooser chooser = new JFileChooser();
				chooser.setMultiSelectionEnabled(true);
				 
				chooser.setCurrentDirectory(new File("D://"));
				int r = chooser.showOpenDialog(ImageViewUI.this);
				if (r == JFileChooser.APPROVE_OPTION) {
					try { 
						Desktop desk=Desktop.getDesktop(); 
						
						 desk.open(new File(chooser.getSelectedFile()
								.getAbsolutePath()));
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}

		});
		JMenuItem exitItem = new JMenuItem("Exit");
		menu.add(exitItem);
		exitItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub

				System.exit(0);
			}

		});

		menuBar.add(menu);
		setJMenuBar(menuBar);

		add(new JScrollPane(fileText), BorderLayout.CENTER);

	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		ImageViewUI ztf = new ImageViewUI();
		ztf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		ztf.setVisible(true);
	}
}
